﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Utilities;

namespace NetTopologySuite.Geometries.Utilities
{
	public class SineStarFactory : GeometricShapeFactory
	{
		public SineStarFactory() : this(new GeometryFactory())
		{
		}

		public SineStarFactory(IGeometryFactory geomFact) : base(geomFact)
		{
			this.NumArms = 8;
			this.ArmLengthRatio = 0.5;
		}

		public int NumArms { get; set; }

		public double ArmLengthRatio { get; set; }

		public IGeometry CreateSineStar()
		{
			Envelope envelope = base.Envelope;
			double num = envelope.Width / 2.0;
			double num2 = this.ArmLengthRatio;
			if (num2 < 0.0)
			{
				num2 = 0.0;
			}
			if (num2 > 1.0)
			{
				num2 = 1.0;
			}
			double num3 = num2 * num;
			double num4 = (1.0 - num2) * num;
			double num5 = envelope.MinX + num;
			double num6 = envelope.MinY + num;
			Coordinate[] array = new Coordinate[base.NumPoints + 1];
			int num7 = 0;
			for (int i = 0; i < base.NumPoints; i++)
			{
				double num8 = (double)i / (double)base.NumPoints * (double)this.NumArms;
				double num9 = num8 - Math.Floor(num8);
				double num10 = (Math.Cos(System.Math.PI*2 * num9) + 1.0) / 2.0;
				double num11 = num4 + num3 * num10;
				double num12 = (double)i * (System.Math.PI*2 / (double)base.NumPoints);
				double x = num11 * Math.Cos(num12) + num5;
				double y = num11 * Math.Sin(num12) + num6;
				array[num7++] = base.CreateCoord(x, y);
			}
			array[num7] = new Coordinate(array[0]);
			ILinearRing shell = this.GeomFact.CreateLinearRing(array);
			return this.GeomFact.CreatePolygon(shell, null);
		}
	}
}
